//@login & register
const express = require("express")
const router = express.Router()

const passport = require("passport")
const multer = require("multer")
const fs = require("fs")

const Adopter = require("../../models/Adopter")
//$route GET api/profiles/test
//@desc  返回的请求的json数据
//@access public
router.get("/test",(req,res)=>{
    res.json({msg:"profile works"})
})

//$route POST api/adopter/add
//@desc  创建信息接口
//@access Private
router.post("/add",passport.authenticate("jwt",{session:false}),(req,res) => {
    const adopterFields = {}
    console.log("reqbody1:",req.body);
    if ( req.body.name) adopterFields.name = req.body.name;
    if (req.body.age) adopterFields.age = req.body.age;
    if (req.body.sex) adopterFields.sex = req.body.sex;
    if (req.body.occupation) adopterFields.occupation = req.body.occupation;
    if ( req.body.address) adopterFields.address = req.body.address;
    if ( req.body.time) adopterFields.time = req.body.time;
    if ( req.body.animal) adopterFields.animal = req.body.animal;
    if ( req.body.phone) adopterFields.phone = req.body.phone;
    console.log(req.body);
    new Adopter(adopterFields).save().then(data => {
        res.json({
            data,
            status:200,
            message:"创建成功！",
        })
    })
})

//$route GET api/adopters
//@desc  查询所有信息接口
//@access Private
router.get("/",passport.authenticate("jwt",{session:false}),(req,res) => {
    Adopter.find()
           .then(data => {
            if(!data){
                return res.status(404).json('没有任何内容')
            }
            res.json({
                data,
                status:200,
                message:"获取数据成功！",
                total:data.length
            })
           })
           .catch(err => res.status(404).json(err))
})

//$route GET api/adopters/:id
//@desc  查询单个信息接口
//@access Private
router.get("/:name",passport.authenticate("jwt",{session:false}),(req,res) => {
    Adopter.find({name:req.params.name})
           .then(data => {
            if(!data){
                return res.status(404).json('没有任何内容')
            }
            res.json({
                data,
                status:200,
                message:"查询数据成功！",
                total:data.length
            })
           })
           .catch(err => res.status(404).json(err))
})


//$route POST api/adopters/edit
//@desc  编辑信息接口
//@access Private
router.post("/edit/:id",passport.authenticate("jwt",{session:false}),(req,res) => {
    const adopterFields = {}
    console.log("reqbody2:",req.body);
    if ( req.body.name) adopterFields.name = req.body.name;
    if (req.body.age) adopterFields.age = req.body.age;
    if (req.body.sex) adopterFields.sex = req.body.sex;
    if (req.body.occupation) adopterFields.occupation = req.body.occupation;
    if ( req.body.address) adopterFields.address = req.body.address;
    if ( req.body.time) adopterFields.time = req.body.time;
    if ( req.body.animal) adopterFields.animal = req.body.animal;
    if ( req.body.phone) adopterFields.phone = req.body.phone;
    
    Adopter.findOneAndUpdate(
        {_id:req.params.id},
        {$set:adopterFields},
        {new:true},
    ).then(data => res.json({
        data,
        status:200,
        message:"编辑成功！",
        total:data.length
    }))

})

//$route POST api/adopters/delete
//@desc  删除信息接口
//@access Private
router.delete("/delete/:id",passport.authenticate("jwt",{session:false}),(req,res) => {
    Adopter.deleteOne({_id:req.params.id})
    .then(profile => res.json({
        status:200,
        message:"删除成功！",
    }))
    .catch(err => res.json({
        status:404,
        message:"删除成功！",
    }))
})
module.exports = router